home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_gen / euphor14.zip / WILDCARD.E < prev    next >
Text File  |  1995-05-13  |  3KB  |  128 lines

  1. -- wildcard.e - wild card matching for strings and DOS file names
  2.  
  3. constant TO_LOWER = 'a' - 'A' 
  4.  
  5. global function lower(sequence x)
  6. -- convert to lower case
  7.     integer c
  8.     
  9.     for i = 1 to length(x) do
  10.     c = x[i]
  11.     if c <= 'Z' then
  12.         if c >= 'A' then
  13.         x[i] = c + TO_LOWER
  14.         end if
  15.     end if
  16.     end for
  17.     return x
  18. end function
  19.  
  20. global function upper(sequence x)
  21. -- convert to upper case
  22.     integer c
  23.     
  24.     for i = 1 to length(x) do
  25.     c = x[i]
  26.     if c >= 'a' then
  27.         if c <= 'z' then
  28.         x[i] = c - TO_LOWER
  29.         end if
  30.     end if
  31.     end for
  32.     return x
  33. end function
  34.  
  35. function qmatch(sequence p, sequence s)
  36. -- find pattern p in string s
  37. -- p may have '?' wild cards (but not '*')
  38.     integer k
  39.     
  40.     if not find('?', p) then
  41.     return match(p, s) -- fast
  42.     end if
  43.     -- must allow for '?' wildcard
  44.     for i = 1 to length(s) - length(p) + 1 do
  45.     k = i
  46.     for j = 1 to length(p) do
  47.         if p[j] != s[k] and p[j] != '?' then
  48.         k = 0
  49.         exit
  50.         end if
  51.         k = k + 1
  52.     end for
  53.     if k != 0 then
  54.         return i
  55.     end if
  56.     end for
  57.     return 0
  58. end function
  59.  
  60. constant END_MARKER = -1
  61.  
  62. global function wildcard_match(sequence pattern, sequence string)
  63. -- returns TRUE if string matches pattern
  64. -- pattern can include '*' and '?' "wildcard" characters
  65.     integer p, f, t 
  66.     sequence match_string
  67.     
  68.     pattern = pattern & END_MARKER
  69.     string = string & END_MARKER
  70.     p = 1
  71.     f = 1
  72.     while f <= length(string) do
  73.     if not find(pattern[p], {string[f], '?'}) then
  74.         if pattern[p] = '*' then
  75.         while pattern[p] = '*' do
  76.             p = p + 1
  77.         end while
  78.         if pattern[p] = END_MARKER then
  79.             return 1
  80.         end if
  81.         match_string = ""
  82.         while pattern[p] != '*' do
  83.             match_string = match_string & pattern[p]
  84.             if pattern[p] = END_MARKER then
  85.             exit
  86.             end if
  87.             p = p + 1
  88.         end while
  89.         if pattern[p] = '*' then
  90.             p = p - 1
  91.         end if
  92.         t = qmatch(match_string, string[f..length(string)])
  93.         if t = 0 then
  94.             return 0
  95.         else
  96.             f = f + t + length(match_string) - 2
  97.         end if
  98.         else
  99.         return 0
  100.         end if
  101.     end if
  102.     p = p + 1
  103.     f = f + 1
  104.     if p > length(pattern) then
  105.         return f > length(string) 
  106.     end if
  107.     end while
  108.     return 0
  109. end function
  110.  
  111. global function wildcard_file(sequence pattern, sequence filename)
  112. -- Return 1 (TRUE) if filename matches the wild card pattern.
  113. -- Similar to DOS wild card matching but better. For example, 
  114. -- "*ABC.*" in DOS will match *all* files, where this function will 
  115. -- only match when the file name part has "ABC" at the end.
  116.     
  117.     pattern = upper(pattern)
  118.     filename = upper(filename)
  119.     if not find('.', pattern) then
  120.     pattern = pattern & '.'
  121.     end if
  122.     if not find('.', filename) then
  123.     filename = filename & '.'
  124.     end if
  125.     return wildcard_match(pattern, filename)
  126. end function
  127.  
  128.